<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JVM类加载机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1rem 1.5rem;
            border-radius: 0.5rem;
            margin: 1rem 0;
        }
        
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            padding: 1.5rem;
            border-radius: 0.5rem;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="text-center">
                <div class="floating-icon inline-block mb-6">
                    <i class="fas fa-cogs text-6xl opacity-90"></i>
                </div>
                <h1 class="text-5xl font-bold mb-4">JVM类加载机制</h1>
                <p class="text-xl opacity-90 max-w-3xl mx-auto">深入理解Java虚拟机的核心机制，掌握类加载的奥秘</p>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">J</span>VM 类加载机制，这个主题看似底层，但它却是理解 Java 运行原理的关键所在。你有没有想过，当你在代码中写下 <code class="bg-gray-200 px-2 py-1 rounded">new</code> 关键字，背后发生了什么？又或者，当你使用第三方库时，JVM 是如何找到这些类的？更重要的是，为什么在某些复杂的项目中，会出现 <code class="bg-gray-200 px-2 py-1 rounded">ClassNotFoundException</code> 或 <code class="bg-gray-200 px-2 py-1 rounded">NoClassDefFoundError</code> 这样令人头疼的问题？
            </p>
            <div class="mt-6 p-6 bg-purple-50 rounded-lg border-l-4 border-purple-500">
                <p class="text-gray-700">
                    <i class="fas fa-lightbulb text-purple-500 mr-2"></i>
                    Java 的设计哲学之一就是"动态加载"。JVM 类加载机制让我们可以在运行时加载、解析和初始化类，从而实现动态扩展和模块化的特性。这种机制不仅是 Java 实现跨平台的基础，也在性能优化、安全性和高可用性上起到了重要作用。
                </p>
            </div>
        </div>

        <!-- Overview Section -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-book-open mr-3 text-purple-600"></i>类加载机制概述
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="bg-gradient-to-br from-blue-50 to-purple-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">
                        <i class="fas fa-rocket text-blue-600 mr-2"></i>类加载的意义
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>动态性</strong>：支持插件式架构和模块化设计</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>按需加载</strong>：优化内存使用，提升性能</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>隔离性和安全性</strong>：避免类冲突，增强系统安全</li>
                    </ul>
                </div>
                
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">
                        <i class="fas fa-layer-group text-purple-600 mr-2"></i>类加载器分类
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-star text-yellow-500 mr-2"></i>启动类加载器（Bootstrap）</li>
                        <li><i class="fas fa-puzzle-piece text-blue-500 mr-2"></i>扩展类加载器（Extension）</li>
                        <li><i class="fas fa-cube text-green-500 mr-2"></i>应用类加载器（Application）</li>
                        <li><i class="fas fa-cog text-purple-500 mr-2"></i>自定义类加载器</li>
                    </ul>
                </div>
            </div>

            <!-- Lifecycle Visualization -->
            <div class="bg-gray-50 p-6 rounded-lg">
                <h3 class="text-xl font-semibold mb-4 text-gray-800">
                    <i class="fas fa-sync-alt text-blue-600 mr-2"></i>类加载生命周期
                </h3>
                <div class="mermaid">
                    graph LR
                        A[加载<br/>Loading] --> B[验证<br/>Verification]
                        B --> C[准备<br/>Preparation]
                        C --> D[解析<br/>Resolution]
                        D --> E[初始化<br/>Initialization]
                        E --> F[使用<br/>Using]
                        F --> G[卸载<br/>Unloading]
                        
                        style A fill:#667eea,stroke:#fff,color:#fff
                        style B fill:#764ba2,stroke:#fff,color:#fff
                        style C fill:#f093fb,stroke:#fff,color:#fff
                        style D fill:#f5576c,stroke:#fff,color:#fff
                        style E fill:#4facfe,stroke:#fff,color:#fff
                        style F fill:#00f2fe,stroke:#fff,color:#fff
                        style G fill:#fa709a,stroke:#fff,color:#fff
                </div>
            </div>
        </div>

        <!-- Lifecycle Details -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-life-ring mr-3 text-purple-600"></i>类加载的生命周期详解
            </h2>
            
            <div class="space-y-6">
                <!-- Loading Phase -->
                <div class="border-l-4 border-blue-500 pl-6">
                    <h3 class="text-2xl font-semibold mb-3 text-gray-800">1. 加载（Loading）</h3>
                    <p class="text-gray-700 mb-3">将类的字节码文件加载到内存中，生成 <code class="bg-gray-200 px-2 py-1 rounded">Class</code> 对象。</p>
                    <div class="bg-blue-50 p-4 rounded-lg">
                        <ul class="space-y-2 text-gray-700">
                            <li><i class="fas fa-arrow-right text-blue-500 mr-2"></i>通过类的全限定名定位字节码文件</li>
                            <li><i class="fas fa-arrow-right text-blue-500 mr-2"></i>读取 .class 文件的二进制内容</li>
                            <li><i class="fas fa-arrow-right text-blue-500 mr-2"></i>在方法区中创建对应的 Class 对象</li>
                        </ul>
                    </div>
                </div>

                <!-- Verification Phase -->
                <div class="border-l-4 border-purple-500 pl-6">
                    <h3 class="text-2xl font-semibold mb-3 text-gray-800">2. 验证（Verification）</h3>
                    <p class="text-gray-700 mb-3">确保加载的字节码文件符合 JVM 规范，保证安全性。</p>
                    <div class="grid md:grid-cols-2 gap-4">
                        <div class="bg-purple-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-purple-700 mb-2">文件格式验证</h4>
                            <p class="text-sm text-gray-600">检查魔数、版本号等</p>
                        </div>
                        <div class="bg-purple-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-purple-700 mb-2">元数据验证</h4>
                            <p class="text-sm text-gray-600">检查继承关系是否合法</p>
                        </div>
                        <div class="bg-purple-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-purple-700 mb-2">字节码验证</h4>
                            <p class="text-sm text-gray-600">检查指令序列的合法性</p>
                        </div>
                        <div class="bg-purple-50 p-4 rounded-lg">
                            <h4 class="font-semibold text-purple-700 mb-2">符号引用验证</h4>
                            <p class="text-sm text-gray-600">确保引用可以被正确解析</p>
                        </div>
                    </div>
                </div>

                <!-- Preparation Phase -->
                <div class="border-l-4 border-pink-500 pl-6">
                    <h3 class="text-2xl font-semibold mb-3 text-gray-800">3. 准备（Preparation）</h3>
                    <p class="text-gray-700 mb-3">为类的静态变量分配内存，并初始化为默认值。</p>
                    <div class="code-block">
                        <pre>class Demo {
    static int x = 10; // 准备阶段时，x = 0
    static final int y = 20; // 常量在准备阶段就会赋值为 20
}</pre>
                    </div>
                </div>

                <!-- Resolution Phase -->
                <div class="border-l-4 border-green-500 pl-6">
                    <h3 class="text-2xl font-semibold mb-3 text-gray-800">4. 解析（Resolution）</h3>
                    <p class="text-gray-700 mb-3">将常量池中的符号引用替换为直接引用。</p>
                    <div class="bg-green-50 p-4 rounded-lg">
                        <p class="text-gray-700">
                            <i class="fas fa-exchange-alt text-green-600 mr-2"></i>
                            符号引用 → 直接引用（内存地